package com.hylanda.pb.service;


import net.minidev.json.JSONObject;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSObject;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.Payload;
import com.nimbusds.jose.crypto.MACSigner;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年5月2日 下午4:09:00 
 * note
 */
public class JWTDemo {
	public static void main(String[] args) {
		JSONObject json = new JSONObject();
//		json.put("appkey", "c0d1b035-d74e-bf72-3233-6439bb89a99c");
//		json.put("secret", "YTM2ZDEzNzVlMmRiNWU1MTM1MWYyYWYxMTcwYzBmNzMzMmI5MmYzZA==");
		json.put("appKey", "6430d670-859d-eb38-b723-862604cad9de");
//		System.out.println(System.currentTimeMillis()+12*3600*1000);
		json.put("exp", (System.currentTimeMillis()+12*3600*1000)/1000);
		Payload payload = new Payload(json);
		JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
//		header.setContentType("JWT");
		final JOSEObjectType JWT = new JOSEObjectType("JWT");
		header.setType(JWT);
//		header.setCustomParameter("typ", "JWT");
//		header.setCustomParameter("secret", "YTM2ZDEzNzVlMmRiNWU1MTM1MWYyYWYxMTcwYzBmNzMzMmI5MmYzZA==");
		// Create JWS object
		JWSObject jwsObject = new JWSObject(header, payload);
		// Create HMAC signer
		JWSSigner signer = new MACSigner("NjE1ZmZkZmM5MDhjY2U0MDRmZWVmNGQ5NTdmYWZkM2Y3YWU2ZDg4OQ==");
		try {
			jwsObject.sign(signer);
		} catch (JOSEException e) {
//			return null;
		}
		String token = jwsObject.serialize();
		System.out.println(token);
		//eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBLZXkiOiI2NDMwZDY3MC04NTlkLWViMzgtYjcyMy04NjI2MDRjYWQ5ZGUiLCJleHAiOjE1MjUyOTk1MDR9.Sq_L3fQQ9Af_VsusoRVeWbc-PnSXTeq7VyZyky72r1Q
//		token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBLZXkiOiIzMWRjNTdlMS00ZTY5LWJlZGItMzc3Yy00MGEzZjQxNTQxYzUiLCJleHAiOjE1MjQ0OTI5NjN9.wSvaeXxEh67tNcCNJTf631MCUpe0EVmc7N-zA43tTZ4";
//		try {
//			ReadOnlyJWTClaimsSet set=JWTParser.
//					parse(token).getJWTClaimsSet();//customClaims={scp=Report.Read, ver=0.2.0, wcn=platform, wid=bfe4e237-81f5-4c28-8056-e2633c647a74, type=embed, rid=c858239f-6afd-40bd-bc32-be001fec09ba}
//			System.out.println(JSON.toJSON(set.getAllClaims()));
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
////		return token;
}

}
